//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Security.Permissions;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace EntLibContrib.Resource.Configuration.Design.Editors
{
  /// <summary>
  /// <para>Provides a user interface for selecting a resource file name.</para>
  /// </summary>
  /// <seealso cref="FileNameEditor"/>
	public abstract class FileResourceFileNameEditor : FileNameEditor
  {
		#region Fields
		private string fileName;
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="FileResourceFileNameEditor"/> class.
		/// </summary>
    protected FileResourceFileNameEditor()
    {
    }
		#endregion

		#region Public Methods
		/// <summary>
		/// Edits the specified object using the editor style provided by the GetEditStyle method.
		/// </summary>
		/// <param name="context">An <para>ITypeDescriptorContext</para> that can be used to gain additional context information.</param>
		/// <param name="provider">A service provider object through which editing services may be obtained.</param>
		/// <param name="value">An instance of the value being edited.</param>
		/// <returns>
		/// The new value of the object. If the value of the object hasn't changed, this should return the same object it was passed.
		/// </returns>
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
      if (provider != null)
      {
				IFileResourceName fileResourceName = (IFileResourceName)value;
				if (!String.IsNullOrEmpty(fileResourceName.PathName))
					fileName = Path.Combine(fileResourceName.PathName, fileResourceName.BaseName + FindFileResourceNameExtension());

				object fileResourceObject = base.EditValue(context, provider, fileName);
				if (fileResourceObject != null)
				{
					string fileResourceFile = (string)fileResourceObject;
					if (!String.IsNullOrEmpty(fileResourceFile))
					{
						fileResourceName.PathName = Path.GetDirectoryName(fileResourceFile);
						fileResourceName.BaseName = Path.GetFileNameWithoutExtension(fileResourceFile);
						if (context != null)
							context.PropertyDescriptor.SetValue(context.Instance, fileResourceName);
						return fileResourceName;
					}
				}
			}
			return value;
		}

		/// <summary>
		/// Gets the file resource name extension.
		/// </summary>
		/// <returns>file name extension</returns>
		public abstract string FindFileResourceNameExtension();

		/// <summary>
		/// Gets the file resource file filter.
		/// </summary>
		/// <returns>Open dialog file filter string</returns>
		public abstract string BuildFileResourceFileFilter();
		#endregion

		#region Protected Methods
		/// <summary>
		/// Initializes the open file dialog when it is created.
		/// </summary>
		/// <param name="openFileDialog">The <see cref="OpenFileDialog"/> to use to select a file name.</param>
    protected override void InitializeDialog(OpenFileDialog openFileDialog)
    {
      base.InitializeDialog(openFileDialog);
			openFileDialog.Filter = BuildFileResourceFileFilter();
      if (!String.IsNullOrEmpty(fileName))
				openFileDialog.InitialDirectory = Path.GetDirectoryName(Path.GetFullPath(fileName));
    }
		#endregion
	}
}